package com.yuan.web.action.lottery;

import java.sql.SQLException;
import java.util.List;
import java.util.ResourceBundle;

import jxl.common.Logger;

import com.yuan.common.action.BaseAction;
import com.yuan.common.mail.SendMail;
import com.yuan.common.message.InformationMessage;
import com.yuan.goods.Goods;
import com.yuan.service.lottery.LotteryService;

/**
 * 0元抽奖的展示页
 * 
 * @author Beni
 * 
 */
public class LotteryAction extends BaseAction {

	private static final long serialVersionUID = -1530524589765348223L;

	private static final Logger logger = Logger.getLogger(LotteryAction.class);

	private List<Goods> goodses;

	private LotteryService lotteryService;

	private List<Integer> size;

	private Goods goods;

	private SendMail sendMail;

	private String tags;

	private static String mailReceiver;

	static {
		ResourceBundle properties = ResourceBundle.getBundle("conf/mail");
		mailReceiver = properties.getString("mail.receiver");
	}

	/**
	 * 显示0元购物
	 * 
	 * @return
	 * @throws Exception
	 */
	public String show() {
		try {
			if (tags == null || tags.isEmpty()) {
				this.setInformationMessage(new InformationMessage("WAR004"));
				return "show";
			}

			goodses = lotteryService.readToday(tags);

			if (goodses == null || goodses.isEmpty()) {
				this.setInformationMessage(new InformationMessage("WAR002"));
			}

		} catch (SQLException ex) {
			logger.error("读取失败", ex);
			this.setInformationMessage(new InformationMessage("ERR001"));
		}

		return "show";
	}

	/**
	 * 展示提供信息页面
	 * 
	 * @return
	 */
	public String provide() {
		return "provide";
	}

	/**
	 * 保存Goods
	 * 
	 * @return
	 */
	public String saveGoods() {
		try {
			sendMail.sendEmail(mailReceiver, "用户提供0元信息", goods.toString());

			return SUCCESS;
		} catch (Exception ex) {
			logger.error("用户提供0元信息失败", ex);
			this.setInformationMessage(new InformationMessage("ERR001"));
		}
		return "provide";
	}

	/**
	 * 显示试用
	 * 
	 * @return
	 */
	public String showTry() {
		try {
			goodses = lotteryService.readToday(tags);

			if (goodses == null || goodses.isEmpty()) {
				this.setInformationMessage(new InformationMessage("WAR002"));
			}

		} catch (SQLException ex) {
			logger.error("读取失败", ex);
			this.setInformationMessage(new InformationMessage("ERR001"));
		}

		return "show";
	}

	public List<Integer> getSize() {
		return size;
	}

	public void setSize(List<Integer> size) {
		this.size = size;
	}

	public List<Goods> getGoodses() {
		return goodses;
	}

	public void setGoodses(List<Goods> goodses) {
		this.goodses = goodses;
	}

	public LotteryService getLotteryService() {
		return lotteryService;
	}

	public void setLotteryService(LotteryService lotteryService) {
		this.lotteryService = lotteryService;
	}

	public Goods getGoods() {
		return goods;
	}

	public void setGoods(Goods goods) {
		this.goods = goods;
	}

	public SendMail getSendMail() {
		return sendMail;
	}

	public void setSendMail(SendMail sendMail) {
		this.sendMail = sendMail;
	}

	public String getTags() {
		return tags;
	}

	public void setTags(String tags) {
		this.tags = tags;
	}

}
